/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.net.SocketFactories;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.net.Socket;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.net.ssl.SSLSocketFactory;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class TestPop3ImapAuth
/*     */   extends TestCase
/*     */ {
/*     */   private static final String CRLF = "\r\n";
/*     */   private static final String HOSTNAME = "localhost";
/*     */   private static final String POP3_CONNECT_RESPONSE = "\\+OK .* POP3 server ready";
/*     */   private static final String POP3_USER = "USER user1\r\n";
/*     */   private static final String POP3_USER_RESPONSE = "\\+OK hello user1, please enter your password";
/*     */   private static final String POP3_PASS = "PASS test123\r\n";
/*     */   private static final String POP3_PASS_RESPONSE = "\\+OK server ready";
/*     */   private static final String POP3_STLS = "STLS\r\n";
/*     */   private static final String POP3_STLS_RESPONSE = "\\+OK Begin TLS negotiation";
/*     */   private static final String POP3_CLEARTEXT_FAILED_RESPONSE = "-ERR only valid after entering TLS mode";
/*     */   private static final String POP3_QUIT = "QUIT\r\n";
/*     */   private static final String POP3_QUIT_RESPONSE = "\\+OK .* closing connection";
/*     */   private static final String POP3_XOIP = "XOIP 100.99.98.97\r\n";
/*     */   private static final String POP3_XOIP_RESPONSE = "\\+OK";
/*     */   private static final String IMAP_CONNECT_RESPONSE = "\\* OK .* Zimbra IMAP4rev1 server ready";
/*     */   private static final String IMAP_LOGIN = "1 LOGIN user1 test123\r\n";
/*     */   private static final String IMAP_LOGIN_RESPONSE = "1 OK.*LOGIN completed";
/*     */   private static final String IMAP_CLEARTEXT_FAILED_RESPONSE = "1 NO cleartext logins disabled";
/*     */   private static final String IMAP_STARTTLS = "2 STARTTLS\r\n";
/*     */   private static final String IMAP_STARTTLS_RESPONSE = "2 OK begin TLS negotiation now";
/*     */   private static final String IMAP_LOGOUT = "3 LOGOUT\r\n";
/*     */   private static final String IMAP_LOGOUT_RESPONSE1 = "\\* BYE.*IMAP4rev1 server closing connection";
/*     */   private static final String IMAP_LOGOUT_RESPONSE2 = "3 OK LOGOUT completed";
/*     */   private static final String IMAP_ID = "4 ID (\"X-ORIGINATING-IP\" \"100.99.98.97\" \"name\" \"foobar\" \"version\" \"1.0\")\r\n";
/*     */   private static final String IMAP_ID_RESPONSE1 = "\\* ID.*";
/*     */   private static final String IMAP_ID_RESPONSE2 = "4 OK ID completed";
/*     */   private Provisioning mProv;
/*     */   private boolean mOrigPop3CleartextLoginEnabled;
/*     */   private boolean mOrigImapCleartextLoginEnabled;
/*     */   private int mPop3CleartextPort;
/*     */   private int mPop3SslPort;
/*     */   private int mImapCleartextPort;
/*     */   private int mImapSslPort;
/*  73 */   private Map<Socket, BufferedReader> mReaders = new HashMap();
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  77 */     SocketFactories.registerProtocols(true);
/*  78 */     this.mProv = Provisioning.getInstance();
/*  79 */     Server server = this.mProv.getLocalServer();
/*  80 */     this.mOrigPop3CleartextLoginEnabled = server.getBooleanAttr("zimbraPop3CleartextLoginEnabled", false);
/*  81 */     this.mOrigImapCleartextLoginEnabled = server.getBooleanAttr("zimbraImapCleartextLoginEnabled", false);
/*  82 */     this.mPop3CleartextPort = server.getIntAttr("zimbraPop3BindPort", 7110);
/*  83 */     this.mPop3SslPort = server.getIntAttr("zimbraPop3SSLBindPort", 7995);
/*  84 */     this.mImapCleartextPort = server.getIntAttr("zimbraImapBindPort", 7143);
/*  85 */     this.mImapSslPort = server.getIntAttr("zimbraImapSSLBindPort", 7995);
/*     */   }
/*     */   
/*     */   public void testPop3CleartextTrue() throws Exception {
/*  89 */     setPop3Cleartext(true);
/*     */     
/*     */ 
/*  92 */     Socket socket = new Socket("localhost", this.mPop3CleartextPort);
/*  93 */     send(socket, "", "\\+OK .* POP3 server ready");
/*  94 */     send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/*  95 */     send(socket, "PASS test123\r\n", "\\+OK server ready");
/*  96 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/*  97 */     socket.close();
/*     */     
/*     */ 
/* 100 */     socket = SocketFactories.dummySSLSocketFactory().createSocket("localhost", this.mPop3SslPort);
/* 101 */     send(socket, "", "\\+OK .* POP3 server ready");
/* 102 */     send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 103 */     send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 104 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/* 105 */     socket.close();
/*     */     
/*     */ 
/* 108 */     socket = new Socket("localhost", this.mPop3CleartextPort);
/* 109 */     send(socket, "", "\\+OK .* POP3 server ready");
/* 110 */     send(socket, "STLS\r\n", "\\+OK Begin TLS negotiation");
/* 111 */     socket = SocketFactories.dummySSLSocketFactory().createSocket(socket, "localhost", this.mPop3CleartextPort, true);
/* 112 */     send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 113 */     send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 114 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/*     */   }
/*     */   
/*     */   public void testPop3CleartextFalse() throws Exception {
/* 118 */     setPop3Cleartext(false);
/*     */     
/*     */ 
/* 121 */     Socket socket = new Socket("localhost", this.mPop3CleartextPort);
/* 122 */     send(socket, null, "\\+OK .* POP3 server ready");
/* 123 */     send(socket, "USER user1\r\n", "-ERR only valid after entering TLS mode");
/* 124 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/* 125 */     socket.close();
/*     */     
/*     */ 
/* 128 */     socket = SocketFactories.dummySSLSocketFactory().createSocket("localhost", this.mPop3SslPort);
/* 129 */     send(socket, null, "\\+OK .* POP3 server ready");
/* 130 */     send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 131 */     send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 132 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/* 133 */     socket.close();
/*     */     
/*     */ 
/* 136 */     socket = new Socket("localhost", this.mPop3CleartextPort);
/* 137 */     send(socket, null, "\\+OK .* POP3 server ready");
/* 138 */     send(socket, "STLS\r\n", "\\+OK Begin TLS negotiation");
/* 139 */     socket = SocketFactories.dummySSLSocketFactory().createSocket(socket, "localhost", this.mPop3CleartextPort, true);
/* 140 */     send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 141 */     send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 142 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/*     */   }
/*     */   
/*     */   public void testImapCleartextTrue() throws Exception {
/* 146 */     setImapCleartext(true);
/*     */     
/*     */ 
/* 149 */     Socket socket = new Socket("localhost", this.mImapCleartextPort);
/* 150 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 server ready");
/* 151 */     send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 152 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server closing connection");
/* 153 */     send(socket, null, "3 OK LOGOUT completed");
/*     */     
/*     */ 
/* 156 */     socket = SocketFactories.dummySSLSocketFactory().createSocket("localhost", this.mImapSslPort);
/* 157 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 server ready");
/* 158 */     send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 159 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server closing connection");
/* 160 */     send(socket, null, "3 OK LOGOUT completed");
/*     */     
/*     */ 
/* 163 */     socket = new Socket("localhost", this.mImapCleartextPort);
/* 164 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 server ready");
/* 165 */     send(socket, "2 STARTTLS\r\n", "2 OK begin TLS negotiation now");
/* 166 */     socket = SocketFactories.dummySSLSocketFactory().createSocket(socket, "localhost", this.mImapCleartextPort, true);
/* 167 */     send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 168 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server closing connection");
/* 169 */     send(socket, null, "3 OK LOGOUT completed");
/*     */   }
/*     */   
/*     */   public void testImapCleartextFalse() throws Exception {
/* 173 */     setImapCleartext(false);
/*     */     
/*     */ 
/* 176 */     Socket socket = new Socket("localhost", this.mImapCleartextPort);
/* 177 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 server ready");
/* 178 */     send(socket, "1 LOGIN user1 test123\r\n", "1 NO cleartext logins disabled");
/* 179 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server closing connection");
/* 180 */     send(socket, null, "3 OK LOGOUT completed");
/*     */     
/*     */ 
/* 183 */     socket = SocketFactories.dummySSLSocketFactory().createSocket("localhost", this.mImapSslPort);
/* 184 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 server ready");
/* 185 */     send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 186 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server closing connection");
/* 187 */     send(socket, null, "3 OK LOGOUT completed");
/*     */     
/*     */ 
/* 190 */     socket = new Socket("localhost", this.mImapCleartextPort);
/* 191 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 server ready");
/* 192 */     send(socket, "2 STARTTLS\r\n", "2 OK begin TLS negotiation now");
/* 193 */     socket = SocketFactories.dummySSLSocketFactory().createSocket(socket, "localhost", this.mImapCleartextPort, true);
/* 194 */     send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 195 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server closing connection");
/* 196 */     send(socket, null, "3 OK LOGOUT completed");
/*     */   }
/*     */   
/*     */   public void testPop3XOIP() throws Exception {
/* 200 */     setPop3Cleartext(true);
/*     */     
/*     */ 
/* 203 */     Socket socket = new Socket("localhost", this.mPop3CleartextPort);
/* 204 */     send(socket, "", "\\+OK .* POP3 server ready");
/* 205 */     send(socket, "XOIP 100.99.98.97\r\n", "\\+OK");
/* 206 */     send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 207 */     send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 208 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/* 209 */     socket.close();
/*     */   }
/*     */   
/*     */   public void testImapID() throws Exception {
/* 213 */     setImapCleartext(true);
/*     */     
/*     */ 
/* 216 */     Socket socket = new Socket("localhost", this.mImapCleartextPort);
/* 217 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 server ready");
/* 218 */     send(socket, "4 ID (\"X-ORIGINATING-IP\" \"100.99.98.97\" \"name\" \"foobar\" \"version\" \"1.0\")\r\n", "\\* ID.*");
/* 219 */     send(socket, null, "4 OK ID completed");
/* 220 */     send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 221 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server closing connection");
/* 222 */     send(socket, null, "3 OK LOGOUT completed");
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 227 */     setPop3Cleartext(this.mOrigPop3CleartextLoginEnabled);
/* 228 */     setImapCleartext(this.mOrigImapCleartextLoginEnabled);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void send(Socket socket, String msg, String responsePattern)
/*     */     throws Exception
/*     */   {
/* 240 */     if (msg != null) {
/* 241 */       OutputStream out = socket.getOutputStream();
/* 242 */       out.write(msg.getBytes());
/* 243 */       out.flush();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 249 */     BufferedReader reader = (BufferedReader)this.mReaders.get(socket);
/* 250 */     if (reader == null) {
/* 251 */       reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/* 252 */       this.mReaders.put(socket, reader);
/*     */     }
/*     */     
/* 255 */     String response = reader.readLine();
/* 256 */     String errorMsg = "Unexpected response: '" + response + "'";
/* 257 */     assertTrue(errorMsg, response.matches(responsePattern));
/*     */   }
/*     */   
/*     */   private void setPop3Cleartext(boolean value) throws Exception {
/* 261 */     setServerAttr("zimbraPop3CleartextLoginEnabled", value);
/*     */   }
/*     */   
/*     */   private void setImapCleartext(boolean value) throws Exception {
/* 265 */     setServerAttr("zimbraImapCleartextLoginEnabled", value);
/*     */   }
/*     */   
/*     */   private void setServerAttr(String attrName, boolean value) throws Exception {
/* 269 */     String val = value ? "TRUE" : "FALSE";
/* 270 */     Map<String, Object> attrs = new HashMap();
/* 271 */     attrs.put(attrName, val);
/* 272 */     this.mProv.modifyAttrs(this.mProv.getLocalServer(), attrs);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 276 */     TestUtil.cliSetup();
/* 277 */     TestUtil.runTest(TestPop3ImapAuth.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestPop3ImapAuth.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */